package com.jh3502.vivo.function.todo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jh3502.vivo.base.BaseCallback;
import com.jh3502.vivo.base.BaseJson;
import com.jh3502.vivo.base.BasePresenter;
import com.jh3502.vivo.base.BaseView;
import com.jh3502.vivo.utils.Utils;

import java.util.List;
import java.util.Map;

/**
 * @author harisucici
 * @date 2017/6/19
 */

public class TodoListPresenter extends BasePresenter<TodoListPresenter.TodoListView> {
    TodoListModel mTodoListModel;

    TodoListView mTodoListView;

    public void setTodoListView(TodoListView mTodoListView) {
        this.mTodoListView = mTodoListView;
    }

    public void toFindTodo() {
        mTodoListView.showLoadingDialog();
        if (mTodoListModel == null) {
            mTodoListModel = new TodoListModel(mTodoListView.getActContext());
        }

        mTodoListModel.findTodo(new BaseCallback(mTodoListView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {
                String s = (String) Utils.getInstance().jso2map(baseJson.getObj().toString()).get("rows");
                List<TodoListBean> dataList = new Gson().fromJson(s,
                        new TypeToken<List<TodoListBean>>() {
                        }.getType());
                mTodoListView.findTodoSuccess(dataList);

            }
        });
    }

    public void toAgreeForAll(List<Map<String, Object>> body) {
        mTodoListView.showLoadingDialog();
        if (mTodoListModel == null) {
            mTodoListModel = new TodoListModel(mTodoListView.getActContext());
        }

        mTodoListModel.agreeForAll(body, new BaseCallback(mTodoListView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {

                mTodoListView.agreeForAllSuccess();
            }
        });
    }


    @Override
    public void cancelCall() {

    }

    public interface TodoListView extends BaseView {
        void findTodoSuccess(List<TodoListBean> dataList);

        void agreeForAllSuccess();
    }

}
